package com.zxl.util.search;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;

public class GetWebContent {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		getPage("http://tz.awangba.com/index.htm?u=sgdh5672", "UTF-8");
//		getPage("http://www.awangba.com/index.php/stat/redirect/u/sgdh5672", "UTF-8");
	}

	public static String getWebCon(String domain) {
		// System.out.println("开始读取内容...("+domain+")");
		StringBuffer sb = new StringBuffer();
		try {
			java.net.URL url = new java.net.URL(domain);
			BufferedReader in = new BufferedReader(new InputStreamReader(url
					.openStream()));
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			in.close();
		} catch (Exception e) {
			System.err.println(e);
		}
		return sb.toString();
	}
	
	/**
	 * 根据网址获取内容
	 * @param page
	 * @param encode
	 * @return
	 */
	public static String getPage(String page, String encode) {
//		System.out.print(new Date() + "\t");
//		System.out.println(page);
		FileUtil.writeFile(Run.content_fileName, new Date() + "\t" + page + "\n", true);
		try {

			URL url = new URL(page);
			HttpURLConnection con = (HttpURLConnection) url.openConnection();

			// 以下是修正Server returned HTTP response code: 403 for URL的代码
			// 通常是因为服务器的安全设置不接受Java程序作为客户端访问，解决方案是设置客户端的User Agent
			con.setRequestProperty("User-Agent", Run.user_agent);
			con.setRequestProperty("Accept-Language", "zh-CN");
			if(Run.referer != null && !Run.referer.equals("")){
				con.setRequestProperty("Referer", Run.referer);
			}
			con.setConnectTimeout(10000);
			con.setReadTimeout(10000);
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					con.getInputStream(), encode));
			StringBuilder b = new StringBuilder();
			String line;
			while ((line = reader.readLine()) != null) {
				b.append(line);
				b.append("\r\n");
			}
			return b.toString();
		} catch (ConnectException ex) {
			System.out.println("Timeout:" + page);
			return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
	
	public static void getHomeTile(String url) throws FailingHttpStatusCodeException, MalformedURLException, IOException{
        final WebClient webClient = new WebClient();
        webClient.addRequestHeader("User-Agent", Run.user_agent);
        webClient.addRequestHeader("Accept-Language", "zh-CN");
        webClient.getPage(url);
//        System.out.println(htmlPage.getTitleText());

    }
}
